'use strict';

/**
 * 字典类型控制器
 */
const Controller = require('egg').Controller;

class DictTypeController extends Controller {
  /**
   * 创建字典类型
   * @return {Promise<void>}
   */
  async create() {
    const { ctx, service } = this;
    const dictTypeData = ctx.request.body;
    const result = await service.dictType.create(dictTypeData);
    ctx.success(result);
  }

  /**
   * 查询字典类型列表
   * @return {Promise<void>}
   */
  async index() {
    const { ctx, service } = this;
    const { page = 1, pageSize = 10, ...query } = ctx.query;
    const result = await service.dictType.list(query, Number(page), Number(pageSize));
    ctx.success(result);
  }

  /**
   * 根据编码查询字典类型
   * @return {Promise<void>}
   */
  async getByCode() {
    const { ctx, service } = this;
    const { code } = ctx.query;
    const result = await service.dictType.findByCode(code);
    ctx.success(result);
  }

  /**
   * 查询单个字典类型
   * @return {Promise<void>}
   */
  async show() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const result = await service.dictType.findById(id);
    ctx.success(result);
  }

  /**
   * 更新字典类型
   * @return {Promise<void>}
   */
  async update() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const dictTypeData = ctx.request.body;
    const result = await service.dictType.update(id, dictTypeData);
    ctx.success(result);
  }

  /**
   * 删除字典类型
   * @return {Promise<void>}
   */
  async destroy() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    await service.dictType.destroy(id);
    ctx.success(null, '删除成功');
  }

  /**
   * 更新字典类型状态
   * @return {Promise<void>}
   */
  async updateStatus() {
    const { ctx, service } = this;
    const { id, status } = ctx.request.body;
    await service.dictType.updateStatus(id, status);
    ctx.success(null, '状态更新成功');
  }

  /**
   * 获取所有启用的字典类型
   * @return {Promise<void>}
   */
  async getAllEnabled() {
    const { ctx, service } = this;
    const result = await service.dictType.getAllEnabled();
    ctx.success(result);
  }
}

module.exports = DictTypeController;
